package com.sunyard.manage.controller;

import com.sunyard.constant.ErrMessageConst;
import com.sunyard.dal.bo.*;
import com.sunyard.dal.bo.role.RoleAuditBO;
import com.sunyard.dal.bo.role.RoleAuditChangeQueryBO;
import com.sunyard.dal.bo.role.RoleAuditQueryBO;
import com.sunyard.dal.dto.RoleCopyDTO;
import com.sunyard.manage.service.RoleAuditService;
import com.sunyard.utils.Response.BaseResult;
import com.sunyard.utils.Response.GetResult;
import com.sunyard.utils.manage.DataGrid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author jiy.fang
 * @ClassName RoleAuditController
 * @Description description
 * @date 2019/7/25 8:39
 */
@Api(tags = "RoleAuditController", description = "角色审核")
@Slf4j
@RestController
@RequestMapping(value = "/roleAudit")
public class RoleAuditController {

    @Autowired
    private RoleAuditService roleAuditService;

    /**
     * @Description: 分页查询审核记录
     * @Author: jiy.fang
     * @Date: 2019/7/18 23:13
     */
    @ApiOperation("分页查询角色审核记录")
    @PostMapping(value = "/showRoleAudit")
    @ResponseBody
    public BaseResult<DataGrid<RoleCopyDTO>> showAuditOrgnizationByPage(HttpServletRequest request, @Valid @RequestBody PaginationBO<RoleAuditQueryBO> roleAuditQuery) {
        try {
            DataGrid<RoleCopyDTO> roleAuditQueryBO= roleAuditService.queryRoleListAuditByPage(request, roleAuditQuery);
            return GetResult.getResult(roleAuditQueryBO);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    /**
     * @Description: 角色审核导出
     * @Author: tsz
     */
    @ApiOperation("角色审核导出")
    @RequestMapping(value = "/roleAuditExport")
    @ResponseBody
    public void roleAuditExport(HttpServletResponse response, @RequestBody RoleAuditQueryBO roleAuditQueryBO, HttpServletRequest request) {
        roleAuditService.roleAuditExport(response,roleAuditQueryBO,request);
    }

    /**
     * @Description: 角色审核
     * @Author: jiy.fang
     * @Date: 2019/7/25 10:34
     */
    @ApiOperation("角色审核")
    @PostMapping(value = "/roleAudit")
    @ResponseBody
    public BaseResult<GetResult> roleAudit(HttpServletRequest request, @Valid @RequestBody RoleAuditBO roleAuditBO) {
        try {
            return roleAuditService.roleAudit(request, roleAuditBO);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    /**
     * @Description: 角色批量审核
     * @Author: jiy.fang
     * @Date: 2019/7/25 10:34
     */
    @ApiOperation("角色批量审核")
    @PostMapping(value = "/batchRoleAudit")
    @ResponseBody
    public BaseResult<GetResult> batchRoleAudit(HttpServletRequest request, @Valid @RequestBody RoleAuditBO roleAuditBO) {
        try {
            return roleAuditService.batchRoleAudit(request, roleAuditBO);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    @ApiOperation("根据审核编号查询审核前后的记录")
    @PostMapping("/showRecordChange")
    public BaseResult<Map<Object, Object>> showRecordChange(@RequestBody RoleAuditChangeQueryBO changeQueryBO) {
        try {
            Map<Object, Object> deviceVariety = roleAuditService.queryRoleRecordChange(changeQueryBO);
            return GetResult.getResult(deviceVariety);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    @ApiOperation("查询角色权限改变")
    @PostMapping("/showRolePopedomChange")
    public BaseResult<Map<String, List<String>>> showRolePopedomChange(@RequestBody RoleAuditChangeQueryBO changeQueryBO) {
        try {
            Map<String, List<String>> deviceVariety = roleAuditService.queryRolePopedomChange(changeQueryBO);
            return GetResult.getResult(deviceVariety);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }
}
